package com.air.ota.vo;

import com.air.ota.util.ConfigProperties;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.PBOCUtil;
import com.air.ota.util.PkgUtil;

public class UTKDepositRequestUplinkVO extends UTKBaseUplinkPgkVO {
	
	//TODO setup the properties
	
	protected byte[] cardAppSequence;
	protected byte[] depositSequence;
	protected byte depositType;
	protected byte keyIndex;//固定0x01  //--
	protected byte[] cardRandom;//随机数1
	protected byte[] ewalletTransSequence;//--
	protected byte[] ewalletRemain;//--
	protected byte[] amount;
	protected byte   transTypeTag;//交易类型标识圈存时为0X02  //--
	protected byte[] terminalCode;//终端机编号                                      //--
	protected byte[] mac1;
	protected byte[] depositInfoFile;
	protected byte[] lastTransData;
	protected byte cardType;//固定0x01 //--
	protected byte[] payPwdOrTriggerSequence;
	protected byte historyTransCount;
	protected byte[] lastHistoryTrans;
	protected byte[] pkgSequence;
	
	protected String password;
	/**
	 * 
	 * @param cardSequence
	 */
	public UTKDepositRequestUplinkVO(byte[] cardSequence){
		super();
		super.cardSequence = cardSequence;
		
		
		cardAppSequence = new byte[8];
		depositSequence = new byte[2];
		cardRandom = new byte[4];
		ewalletTransSequence = new byte[2];
		ewalletRemain = new byte[4];
		amount = new byte[4];
		terminalCode = new byte[6];
		mac1 = new byte[4];
		depositInfoFile = new byte[28];
		lastTransData = new byte[23];
		payPwdOrTriggerSequence = new byte[8];
		
		pkgSequence = new byte[4];
		lastHistoryTrans = new byte[14];
		
		transTypeTag = 0x02;
		
		//TODO setup the properties
	}
	
	public void parse(byte[] pkg){
		super.parse(pkg);
		
		//System.arraycopy(pkg, 21, this.cmdLength, 0, 2);
		
		//int encCmdDataLength = pkg.length-23-8;//去头24byte，去mac和random8byte
		//encCmdData = new byte[encCmdDataLength];
		//System.arraycopy(pkg, 23, encCmdData, 0, encCmdDataLength);
		
		super.decryptCmdData();
		
		System.out.println(HexStringUtil.bytesToHexString(this.cmdData));
		
		System.arraycopy(cmdData, 0, cardAppSequence, 0, 8);
		System.arraycopy(cmdData, 8, depositSequence, 0, 2);
		depositType = cmdData[10];
		keyIndex=0x01;
		System.arraycopy(cmdData, 11, this.cardRandom, 0, 4);
		System.arraycopy(cmdData, 15, this.ewalletRemain, 0, 4);
		System.arraycopy(cmdData, 19, this.amount, 0, 4);
		System.arraycopy(cmdData, 23, this.ewalletTransSequence, 0, 2);
		
		
		System.arraycopy(cmdData, 25, this.mac1, 0, 4);
		System.arraycopy(cmdData, 29, this.depositInfoFile, 0, 28);
		System.arraycopy(cmdData, 57, this.lastTransData, 0, 23);
		cardType = 0x1C;
		System.arraycopy(cmdData, 80, this.payPwdOrTriggerSequence, 0, 8);
		historyTransCount = cmdData[88];
		int historyTransLen = (int)historyTransCount*14;
		
		System.arraycopy(cmdData, 89, this.lastHistoryTrans, 0, historyTransLen);
		System.arraycopy(cmdData, 89+historyTransLen, this.pkgSequence, 0, 4);
		PkgUtil.setCurrentPkgSequence(pkgSequence);
		
		
		//System.arraycopy(this.depositInfoFile, 0, this.ewalletTransSequence, 0, 2);
		//ewalletTransSequence = makeEwalletTransSequence();
		//ewalletTransSequence = depositSequence;
		
		
		//System.arraycopy(depositInfoFile, 2, this.ewalletRemain, 0, 4);
		transTypeTag = depositInfoFile[10];
		//System.arraycopy(depositInfoFile, 11, this.terminalCode, 0, 6);
		this.terminalCode = HexStringUtil.hexStringToBytes(ConfigProperties.getInstance().getValue("szt.terminalNum")); 
		//ewalletRemain = getBeforeDepositRemain();//改成充值前余额
	     
		PBOCUtil pboc = new PBOCUtil();
		this.password = HexStringUtil.bytesToHexString(pboc.TripleDes(this.UTKSessionKey, this.payPwdOrTriggerSequence, 0));
	}
	
	private byte[] makeEwalletTransSequence(){
		byte[] lastSequence = new byte[2];
		System.arraycopy(this.depositInfoFile, 0, lastSequence, 0, 2);
		/*int seqInt=Integer.parseInt(HexStringUtil.bytesToHexString(lastSequence), 16);
		seqInt++;
		String hexString=Integer.toHexString(seqInt);
		int fillCount = 4-hexString.length();
		for(int i=0;i<fillCount;i++){
			hexString = "0"+hexString;
		}
		return HexStringUtil.hexStringToBytes(hexString);*/
		return lastSequence;
	}
	
	public byte[] getBeforeDepositRemain(){
		
		long afterRemain=Long.parseLong(HexStringUtil.bytesToHexString(ewalletRemain), 16);
		long iAmount=Long.parseLong(HexStringUtil.bytesToHexString(amount), 16);
		long beforeRemain = afterRemain-iAmount;
		String hexStr=Long.toHexString(beforeRemain);
		int fillCount = 8-hexStr.length();
		for(int i=0;i<fillCount;i++){
			hexStr= "0"+hexStr;
		}
		
		return HexStringUtil.hexStringToBytes(hexStr);
	}
	
	public byte[] getCardAppSequence() {
		return cardAppSequence;
	}
	
	public byte[] getDepositSequence() {
		return depositSequence;
	}
	
	public byte getDepositType() {
		return depositType;
	}

	public byte getKeyIndex() {
		return keyIndex;
	}

	public byte[] getCardRandom() {
		return cardRandom;
	}

	public byte[] getEwalletTransSequence() {
		return ewalletTransSequence;
	}

	public byte[] getEwalletRemain() {
		return ewalletRemain;
	}

	public byte[] getAmount() {
		return amount;
	}

	public byte getTransTypeTag() {
		return transTypeTag;
	}

	public byte[] getTerminalCode() {
		return terminalCode;
	}

	public byte[] getMac1() {
		return mac1;
	}
	
	public void setDepositInfoFile(byte[] file) {
		this.depositInfoFile=file;
	}
	
	public byte[] getDepositInfoFile() {
		return depositInfoFile;
	}

	public byte[] getLastTransData() {
		return lastTransData;
	}

	public byte getCardType() {
		return cardType;
	}

	public byte[] getPayPwdOrTriggerSequence() {
		return payPwdOrTriggerSequence;
	}

	

	public byte[] getPkgSequence() {
		return pkgSequence;
	}
	
	public String getPassword(){
		return this.password;
	}
	
	public byte[] getLastHistoryTrans(){
		return this.lastHistoryTrans;
	}
}
